import React from 'react';
import {Route,Redirect} from 'react-router-dom' 
import Login from '../pages/login';
import User from '../pages/user';

export default class Auth extends React.Component{
    state={
        hasAuth:false,
        auth:false,
        data:{}
    }
    async componentDidMount(){
        let strem = await fetch('/data/user.json')
        let data = await strem.json()
        this.setState({
            data: data.username,
            auth: data.auth,
            hasAuth: true
        })
    }
    render(){
        let {component}=this.props
        let {hasAuth,auth,data}=this.state
        if(!hasAuth) return null
        if(auth){
        return <Route component={component}></Route>
           
        } else {
        return <Redirect to="/login"></Redirect>

        }
    }
}